home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / c01lab1.zip / ADAWKBK / SOL4-2.ADA < prev    next >
Text File  |  1992-11-11  |  2KB  |  88 lines

  1. -- Problem 4.2
  2. -- by Rick Conn
  3. package Complex is
  4.  
  5.   type NUMBER is private;
  6.  
  7.   function Set (RP, IP : in FLOAT) return NUMBER;
  8.   function "+" (Left, Right : in NUMBER) return NUMBER;
  9.   function "-" (Left, Right : in NUMBER) return NUMBER;
  10.   function "*" (Left, Right : in NUMBER) return NUMBER;
  11.   procedure Print (Item : in NUMBER);
  12.  
  13. private
  14.  
  15.   type NUMBER is record
  16.     RP : FLOAT := 0.0;  -- initialize to 0,0
  17.     IP : FLOAT := 0.0;
  18.   end record;
  19.  
  20. end Complex;
  21.  
  22. with Text_IO;
  23. package body Complex is
  24.  
  25.   package Float_IO is new Text_IO.Float_IO (FLOAT);
  26.  
  27.   function Set (RP, IP : in FLOAT) return NUMBER is
  28.   begin
  29.     return (RP, IP);
  30.   end Set;
  31.  
  32.   function "+" (Left, Right : in NUMBER) return NUMBER is
  33.   begin
  34.     return (Left.RP + Right.RP, Left.IP + Right.IP);
  35.   end "+";
  36.  
  37.   function "-" (Left, Right : in NUMBER) return NUMBER is
  38.   begin
  39.     return (Left.RP - Right.RP, Left.IP - Right.IP);
  40.   end "-";
  41.  
  42.   function "*" (Left, Right : in NUMBER) return NUMBER is
  43.   begin
  44.     return (Left.RP * Left.RP - Right.RP * Right.RP,
  45.             Left.RP * Right.IP + Left.IP * Right.RP);
  46.   end "*";
  47.  
  48.   procedure Print (Item : in NUMBER) is
  49.   begin
  50.     Float_IO.Put (Item.RP, 5, 5, 0);
  51.     Text_IO.Put (" + ");
  52.     Float_IO.Put (Item.IP, 5, 5, 0);
  53.     Text_IO.Put ("j");
  54.   end Print;
  55.  
  56. end Complex;
  57.  
  58. with Text_IO;
  59. with Complex;
  60. use Complex;  -- so infix operators may be used
  61. procedure Main is
  62.  
  63.   N1, N2, N3 : Complex.NUMBER;
  64.  
  65.   procedure Print_All is
  66.   begin
  67.     Text_IO.Put ("N1   = "); Complex.Print (N1); Text_IO.New_Line;
  68.     Text_IO.Put ("  N2 = "); Complex.Print (N2); Text_IO.New_Line;
  69.     Text_IO.Put ("  N3 = "); Complex.Print (N3); Text_IO.New_Line;
  70.   end Print_All;
  71.  
  72. begin -- Main
  73.  
  74.   Print_All;
  75.   N1 := Set (2.2, 4.4);
  76.   N2 := Set (1.0, 12.2);
  77.   N3 := N1 + N2;
  78.   Text_IO.Put_Line ("Sum:");
  79.   Print_All;
  80.   N3 := N1 - N2;
  81.   Text_IO.Put_Line ("Difference:");
  82.   Print_All;
  83.   N3 := N1 * N2;
  84.   Text_IO.Put_Line ("Product:");
  85.   Print_All;
  86.  
  87. end Main;
  88.